import axios from 'axios'
import store from '../store'
import vue from 'vue'
import Cookies from 'js-cookie'


let baseUrl = process.env.AJAX_PATH;

// 创建axios实例
const service = axios.create({
    baseURL: baseUrl, // api的base_url
    timeout: 5000,         // 请求超时时间
    withCredentials:true
})

// request拦截器
service.interceptors.request.use(config => {
    config.headers['loginType'] = 'api';
    return config
}, error => {
    Promise.reject(error)
})

// respone拦截器
service.interceptors.response.use(
    (response) => {
        console.log(response);
        if (response.data.status != undefined && response.data.status !== 1) {
            if (response.data.status == 401) {
                store.dispatch('FedLogOut').then(() => {
                    // this.$router.push({path:'/login'});
                    // location.reload()// 为了重新实例化vue-router对象 避免bug
                })
            }
            if (response.data.status == 401) {

            }
        } else {
            return response.data;
        }
    },
    error => {
    }
)

export {baseUrl}
export default service
